package com.mxpio.erp.equipment.controller;


import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.equipment.entity.UpkeepReturnBomNotice;
import com.mxpio.erp.common.equipment.service.UpkeepReturnBomNoticeService;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.order.service.NoticeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "UpkeepReturnBomNoticeController", description = "保养退料通知单接口")
@RestController
@RequestMapping("/erp/equipment/upkeep/returnnotice/")
public class UpkeepReturnBomNoticeController {

    @Autowired
    private UpkeepReturnBomNoticeService service;

    @Autowired
    private NoticeService noticeService;
    @GetMapping("list")
    @Operation(summary = "保养退料通知单列表", description = "获取保养退料通知单列表（无分页）", method = "GET")
    public Result<List<UpkeepReturnBomNotice>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<UpkeepReturnBomNotice> noticeList = service.list(UpkeepReturnBomNotice.class, criteria);
        return Result.OK(noticeList);
    }
    @GetMapping("page")
    @Operation(summary = "保养退料通知单列表", description = "获取保养退料通知单列表（分页）", method = "GET")
    public Result<Page<UpkeepReturnBomNotice>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<UpkeepReturnBomNotice> noticeList = service.listPage(UpkeepReturnBomNotice.class, page, criteria);
        return Result.OK(noticeList);
    }

    @GetMapping("list/{noticeNo}")
    @Operation(summary = "根据noticeNo获取保养退料通知单", description = "根据noticeNo获取保养退料通知单", method = "GET")
    public Result<UpkeepReturnBomNotice> getById(@Parameter(description="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
        UpkeepReturnBomNotice noticeList = service.getById(UpkeepReturnBomNotice.class, noticeNo);
        return Result.OK(noticeList);
    }

    @DeleteMapping("remove/{noticeNos}")
    @Operation(summary = "根据noticeNo删除保养退料通知单", description = "删除保养退料通知单", method = "DELETE")
    public Result<UpkeepReturnBomNotice> remove(@Parameter(description="业务单号，多个单号用英文逗号分隔") @PathVariable(name = "noticeNos", required = true) String noticeNos) {
        String[] noticeNo = noticeNos.split(",");
        for(String key : noticeNo){
            service.delete(UpkeepReturnBomNotice.class, key);
        }
        return Result.OK();
    }

    @PutMapping("reject/{noticeNo}")
    @Operation(summary = "拒绝通知单", description = "拒绝通知单", method = "PUT")
    public Result<? extends Notice> close(@Parameter(description="通知单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
        UpkeepReturnBomNotice notice = service.getById(UpkeepReturnBomNotice.class,noticeNo);
    	return noticeService.updateNoticeStatus(notice, NoticeStatusEnums.NoticeStatus.REJECT);
    }
}
